﻿using Puddy.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Puddy
{
    public class FoursquareServiceHandler
    {
        public event Action<List<Place>> OnGetPlace;

        public void searchByFoursquare(String name, String lat, String lng)
        {

            DateTime date = DateTime.Now;
            String dateStr = date.ToString("yyyyMMdd");

            String urlFoursquare = "https://api.foursquare.com/v2/venues/search?ll="
              + lat
              + ","
              + lng
              + "&query="
              + name.Trim()
              + "&oauth_token=3UYF4J2U30CO0KJEE44GX2SCRH13CVEUIF1IRWT11HOUBFCO&v="
              + dateStr;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GetLocation_DownloadStringCompleted);

            client.DownloadStringAsync(new Uri(urlFoursquare));

        }

        void GetLocation_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Error != null)
                {
                    OnGetPlace(null);
                    Debug.WriteLine(e.Error.Message);
                    return;
                }
                else
                {
                    Debug.WriteLine(e.Result);

                    List<Place> listLocation = new List<Place>();



                    FoursquarePlaceResponce foursquarePlaces = FoursquarePlaceResponce.ParseFromJSON(e.Result);

                    int count = 0;
                    List<Place> places = new List<Place>();

                    foreach (FoursquarePlace foursquarePlace in foursquarePlaces.Renues.Places)
                    {
                        Place place = new Place();

                        place.Address = foursquarePlace.Location.Address;
                        if (foursquarePlace.Location.City != null)
                        {
                            place.Address += "," + foursquarePlace.Location.City;
                        }
                        else
                        {
                            if (foursquarePlace.Location.State != null)
                            {
                                place.Address += "," + foursquarePlace.Location.State;
                            }
                        }

                        if (foursquarePlace.Location.Country != null)
                        {
                            place.Address += "," + foursquarePlace.Location.Country;
                        }

                        place.Name = foursquarePlace.Name;

                        //cheat:
                        if (place.Address.Length < place.Name.Length)
                        {
                            place.Address = place.Name;
                        }

                        place.Lattitude = foursquarePlace.Location.Lattitude;
                        place.Longitude = foursquarePlace.Location.Longitude;

                        places.Add(place);

                        count++;
                        if (count >= 8)
                            break;
                    }

                    PrintPlaceName(places);

                    OnGetPlace(places);


                    

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void PrintPlaceName(List<Place> places)
        {
            foreach (var p in places)
            {
                Debug.WriteLine("PLACE NAME: " + p.Name);
            }
        }
    }
}
